<?php

abstract  class  Score_Model_Score
{

    /**
     * 配課科目流水號
     */
    protected $_sn = 0;

    /**
     * 教師配課資料
     * @var Doctrine_Collection
     */
    protected $_teacherCourse;

    /**
     *  科目設定資料
     * @var Doctrine_Collection
     */
    protected $_subjectSet;

    /**
     * 成績評量相關設定
     * @var Doctrine_Collection
     */
    protected $_examine;

    /**
     * 目前階段
     * @var integer
     */
    protected $_currStage;

    /**
     *  分組班課程
     * @var unknown_type
     */
    protected $_isElective = false;


    /**
     * @param int $sn 配課代號
     * @param int $stage 階段次
     */
    public function __construct($sn)
    {
        $this->setSn($sn);
    }

    /**
     * 設定 配課資料
     * @param int $sn
     */
    public function setSn($sn)
    {
        // 檢查是否為分組班課程
        if ($pos=strpos($sn,'-')) {
            $sn = substr($sn,$pos+1);
            $this->_isElective = true;
        }
        // 教師配課資料
        $this->_sn = $sn;
        $semesterTeacherCourse  = new Semester_Model_SemesterTeacherCourse();
        $this->_teacherCourse = $semesterTeacherCourse->getTable()->find($sn);
        // 查詢該科目設定資料
        $q = Doctrine_Query::create()
        ->select('a.*,b.name AS subject_name, b.scope AS scope_name')
        ->from('Semester_Model_SemesterSubject a')
        ->leftJoin('a.Semester_Model_SemesterCourse b')
        ->where('a.year=?',  $this->_teacherCourse->year)
        ->andWhere('a.semester=?',  $this->_teacherCourse->semester)
        ->andWhere('a.grade=?',  $this->_teacherCourse->grade)
        ->andWhere('a.class_sn=?',  $this->_teacherCourse->class_sn)
        ->andWhere('a.subject_sn=?',  $this->_teacherCourse->subject_sn);
        $this->_subjectSet = $q->fetchOne();
        //查詢該學科考次,定期平時比率
        $semesterExamine = new Semester_Model_SemesterExamine();
        $this->_examine = $semesterExamine->getExaminSet($this->_teacherCourse->year,
        $this->_teacherCourse->semester, $this->_teacherCourse->grade);

    }

    /**
     *  配課代號
     * @return unknown_type
     */
    public function getSn()
    {
        return $this->_sn;
    }

    /**
     * 取得教師配課資料$totalRatio
     * @return  Doctrine_Collection
     */
    public function getTeacherCourse()
    {
        if ($this->_sn)
        return $this->_teacherCourse;
        else
        return false;
    }

    /**
     * 科目設定資料
     * @return Doctrine_Collection
     */
    public function getSubjectSet()
    {
        if ($this->_sn)
        return $this->_subjectSet;
        else
        false;
    }

    /**
     * 是否為分組班課程
     * @return boolean
     */
    public function isElective()
    {
        return $this->_isElective;
    }

    /**
     *  成績考查相關設定
     * @return Doctrine_Collection
     */
    public function getExamine()
    {
        if ($this->_sn)
        return $this->_examine;
        else
        return false;
    }

    /**
     *  目前輸入的階段
     * @return integer
     */
    public function getCurrStage()
    {
        return $this->_currStage;
    }



    /**
     * 成績是否傳送
     * @return unknown_type
     */
    public function getSendmit()
    {
        $q = Doctrine_Query::create()
        ->select('sendmit');
        if ($this->_isElective)
        $q->from('Student_Model_StudentScoreElectiveStage');
        else {
            $q->from('Student_Model_StudentScoreStage')
            ->andWhere('class_sn=?',  $this->_teacherCourse->class_sn)
            ->andWhere('subject_sn=?',  $this->_teacherCourse->subject_sn);
        }
        $q->andWhere('test_sort=?', $this->_currStage);

        $res = $q->fetchOne();
        $sendmit = false;
        if ($res)
        $sendmit = ($res->sendmit=='Y')?true:false;
        return $sendmit;
    }

    /**
     *   本學期學生名冊
     * @return unknown_type
     */
    public function getStudents()
    {
        if ($this->_isElective) {
            $q = Doctrine_Query::create()
            ->select('s.number AS number , a.name, a.sex, a.sn ,a.study_condition ,
            b.student_id AS student_id, b.class_sn AS class_sn ,b.number AS classNumber')
            ->from('Student_Model_Student a ')
            ->leftJoin('a.Student_Model_StudentSemester b ')
            ->leftJoin('Student_Model_StudentScoreElectiveStudent s')
            ->leftJoin('Student_Model_StudentScoreElectiveGroup g')
            ->where('a.sn = s.student_sn AND s.group_sn=g.sn AND g.teacher_course_sn=?',
            $this->_teacherCourse->sn )
            ->andwhere ('b.year=?',$this->_teacherCourse->year)
            ->andWhere('b.semester=?',$this->_teacherCourse->semester)
            ->orderBy('s.number, b.class_sn,b.number');

            $res = $q->fetchArray();
            $arr = array();
            foreach($res as $row) {
                if ($row['number'] == 0)
                $row['number'] = $row['class_sn'].'-'.$row['classNumber'];
                $sn = $row['sn'];
                $row['sn'] = $row['class_sn'].'-'.$row['sn'];
                $arr[$sn] = $row;
            }
            return $arr;

        }
        else {
            $student = new Student_Model_Student();

            $students = $student->getClassStudent(
            $this->_teacherCourse->year,
            $this->_teacherCourse->semester,
            $this->_teacherCourse->grade,
            $this->_teacherCourse->class_sn
            );
        }
        return $students;
    }
}